package com.intco.ait.quotation.api.starter.web;

import com.intco.ait.quotation.api.common.model.BaseResponse;
import com.intco.ait.quotation.api.common.model.PackageMethodVo;
import com.intco.ait.quotation.api.common.model.request.PackageSearchVo;
import com.intco.ait.quotation.api.infrastructure.service.PackageService;
import com.intco.ait.quotation.api.starter.advice.LocalException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/package")
@Slf4j
public class PackageController {

    @Autowired
    private PackageService packageService;

    @PostMapping("/search")
    public BaseResponse<?> getPackageList(@RequestBody PackageSearchVo packageSearch) {
        try {
            return BaseResponse.success(packageService.search(packageSearch));
        } catch (Exception e) {
            log.error("getPackageList调用失败", e);
            throw new LocalException(500, "查询失败:".concat(e.getMessage()), e);
        }
    }

    @PostMapping("/add")
    public BaseResponse<?> createPackage(@RequestBody PackageMethodVo packageMethodVo) {
        try {
            packageService.createPackage(packageMethodVo);
            return BaseResponse.success();
        } catch (Exception e) {
            log.error("createPackage调用失败", e);
            throw new LocalException(500, "新增失败:".concat(e.getMessage()), e);
        }
    }

    @PostMapping("/edit")
    public BaseResponse<?> updatePackage(@RequestBody PackageMethodVo packageMethodVo) {
        try {
            packageService.update(packageMethodVo);
            return BaseResponse.success();
        } catch (Exception e) {
            log.error("updatePackage调用失败", e);
            throw new LocalException(500, "修改失败:".concat(e.getMessage()), e);
        }
    }
}
